package com.cvte.vdt.uartservice.util;

import android.os.RemoteCallbackList;
import android.os.RemoteException;

import com.cvte.vdt.uartservice.aidl.IVDTUartCallback;
import com.cvte.vdtlibrary.bluetooth.EntityBluetoothDevice;
import com.cvte.vdtlibrary.bluetooth.EnumBTRecModeStatus;
import com.cvte.vdtlibrary.bluetooth.EnumBTSendModeStatus;
import com.cvte.vdtlibrary.bluetooth.EnumBluetoothKey;
import com.cvte.vdtlibrary.bluetooth.EnumBluetoothMode;

import java.util.List;
import java.util.Objects;

/**
 * Created by user on 2018/12/14.
 */
public class CallbackUtil {
    private static Object lock = new Object();
    private static RemoteCallbackList<IVDTUartCallback> mCallbacks = null;
    public static void start(){
        mCallbacks = new RemoteCallbackList<IVDTUartCallback>();
    }
    public static void kill(){
        if(mCallbacks != null) {
            mCallbacks.kill();
        }
    }
    public static void register(IVDTUartCallback ivdtUartCallback){
        if(mCallbacks != null) {
            mCallbacks.register(ivdtUartCallback);
        }
    }
    public static void unregister(IVDTUartCallback ivdtUartCallback){
        if(mCallbacks != null) {
            mCallbacks.unregister(ivdtUartCallback);
        }
    }
    public static void callbackOnGetBtVolume(EnumBluetoothMode mode, int volume){
        if(mCallbacks != null){
            synchronized (lock) {
                final int N = mCallbacks.beginBroadcast();
                for (int i = 0; i < N; i++) {
                    try {
                        mCallbacks.getBroadcastItem(i).onGetBtVolume(mode, volume);
                    } catch (RemoteException e) {
                    }
                }
                mCallbacks.finishBroadcast();
            }
        }
    }
    public static void callbackOnGetBtVersion(EnumBluetoothMode mode,String ver){
        if(mCallbacks != null){
            synchronized (lock) {
                final int N = mCallbacks.beginBroadcast();
                for (int i = 0; i < N; i++) {
                    try {
                        mCallbacks.getBroadcastItem(i).onGetBtVersion(mode, ver);
                    } catch (RemoteException e) {
                    }
                }
                mCallbacks.finishBroadcast();
            }
        }
    }
    public static void callbackOnBluetoothSendModeKey(EnumBluetoothKey key){
        if(mCallbacks != null){
            synchronized (lock) {
                final int N = mCallbacks.beginBroadcast();
                for (int i = 0; i < N; i++) {
                    try {
                        mCallbacks.getBroadcastItem(i).onBluetoothSendModeKey(key);
                    } catch (RemoteException e) {
                    }
                }
                mCallbacks.finishBroadcast();
            }
        }
    }
    public static void callbackOnBluetoothRecModeStatusChange(EnumBTRecModeStatus status, EntityBluetoothDevice dev, List<EntityBluetoothDevice> list){
        if(mCallbacks != null){
            synchronized (lock) {
                final int N = mCallbacks.beginBroadcast();
                for (int i = 0; i < N; i++) {
                    try {
                        mCallbacks.getBroadcastItem(i).onBluetoothRecModeStatusChange(status, dev, list);
                    } catch (RemoteException e) {
                    }
                }
                mCallbacks.finishBroadcast();
            }
        }
    }
    public static void callbackOnBluetoothSendModeStatusChange(EnumBTSendModeStatus status, EntityBluetoothDevice dev, List<EntityBluetoothDevice> list){
        if(mCallbacks != null){
            synchronized (lock) {
                final int N = mCallbacks.beginBroadcast();
                for (int i = 0; i < N; i++) {
                    try {
                        mCallbacks.getBroadcastItem(i).onBluetoothSendModeStatusChange(status, dev, list);
                    } catch (RemoteException e) {
                    }
                }
                mCallbacks.finishBroadcast();
            }
        }
    }

    public static void callbackonBluetoothSendDataChange( List<EntityBluetoothDevice> list){
        if(mCallbacks != null){
            synchronized (lock) {
                final int N = mCallbacks.beginBroadcast();
                for (int i = 0; i < N; i++) {
                    try {
                        mCallbacks.getBroadcastItem(i).onBluetoothSendDataChange(list);
                    } catch (RemoteException e) {
                    }
                }
                mCallbacks.finishBroadcast();
            }
        }
    }

    public static void callbackOnBluetoothRecMusicNameChange(String name){
        if(mCallbacks != null){
            synchronized (lock) {
                final int N = mCallbacks.beginBroadcast();
                for (int i = 0; i < N; i++) {
                    try {
                        mCallbacks.getBroadcastItem(i).onBluetoothRecMusicNameChange(name);
                    } catch (RemoteException e) {
                    }
                }
                mCallbacks.finishBroadcast();
            }
        }
    }
    public static void callbackOnBluetoothRecConnectedNameChange(String name){
        if(mCallbacks != null){
            synchronized (lock) {
                final int N = mCallbacks.beginBroadcast();
                for (int i = 0; i < N; i++) {
                    try {
                        mCallbacks.getBroadcastItem(i).onBluetoothRecConnectedNameChange(name);
                    } catch (RemoteException e) {
                    }
                }
                mCallbacks.finishBroadcast();
            }
        }
    }
}
